﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MedManager.BLL;
using MedManager.Model;
using MedManager.Src;
using Maticsoft.DBUtility;

namespace MedManager
{

    public partial class UserManager : Form
    {
        public UserManager()
        {
            InitializeComponent();
        }

        private userinfoBLL userinfoBll = new userinfoBLL();
        private userinfoEntity userTemp=new userinfoEntity();
        private int userID;
        


        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            List<ListItem> list = new List<ListItem>();
            list.Add(new ListItem("1", "门诊用户"));
            list.Add(new ListItem("2", "仓库用户"));
            cmbUserType.DisplayMember = "Text";
            cmbUserType.ValueMember = "Value";
            cmbUserType.DataSource = list;
            cmbUserType.SelectedValue = "1";
        }

        private void Clear()
        {
            userID = -1;
            userTemp = null;
            txtUserName.Text = "";
            txtPassword.Text = "";
            cmbUserType.SelectedIndex = 0;
            btnSave.Enabled = false;

        }

        private void BindData()
        {
            BindData("");
        }

        private void BindData(string username)
        {
            StringBuilder sql=new StringBuilder("select id as '编号',username as '用户名',password as '密码',case type when 1 then '门诊用户' when 2 then '仓库用户' end as '用户类型' ");
            sql.Append(" from userinfo where type!=0 and status=0");
            if(String.IsNullOrEmpty(username)==false)
            {
                sql.Append(" and username like '%" + username + "%'");
            }
            DataSet ds=DbHelperSQL.Query(sql.ToString());
            dgvUserInfo.DataSource = ds.Tables[0];
        }

        /// <summary>
        /// 检查用户名是否重复
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns>结果</returns>
        private bool CheckUserName(string username)
        {
            bool flag = false;
            DataSet ds = GetUserByName(username);
            if (ds.Tables[0].Rows.Count == 0)
            {
                flag = true;
            }
            return flag;
        }

        private DataSet GetUserByName(string username)
        {
            string sql = "select * from userinfo where username=@username and status=0";
            SqlParameter[] sqlParameters = new SqlParameter[1];
            sqlParameters[0] = new SqlParameter("@username", username);
            DataSet ds = DbHelperSQL.Query(sql, sqlParameters);
            return ds;
        }

        private bool CheckInput()
        {
            bool flag = true;
            if(String.IsNullOrEmpty(txtUserName.Text)||string.IsNullOrEmpty(txtPassword.Text))
            {
                flag = false;
            }
            return flag;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if(CheckInput()==true)
            {
                userTemp = userinfoBll.GetModel(userID);

                userTemp.password = txtPassword.Text.Trim();
                ListItem selectedItem = (ListItem)cmbUserType.SelectedItem;
                userTemp.type = Convert.ToInt32(selectedItem.Value);
                bool result=userinfoBll.Update(userTemp);
                if(result==true)
                {
                    MessageBox.Show("保存成功！");
                    BindData();
                }else
                {
                    MessageBox.Show("保存失败！");
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if(CheckInput()==true)
            {
                if (CheckUserName(txtUserName.Text.Trim()) == true)
                {
                    userinfoEntity userInfo = new userinfoEntity();
                    userInfo.username = txtUserName.Text.Trim();
                    userInfo.password = txtPassword.Text.Trim();
                    ListItem selectedItem = (ListItem)cmbUserType.SelectedItem;
                    userInfo.type = Convert.ToInt32(selectedItem.Value);;
                    int result = userinfoBll.Add(userInfo);
                    if (result != 0)
                    {
                        MessageBox.Show("添加成功!" + result);
                        BindData();
                    }else
                    {
                        MessageBox.Show("添加失败");
                    }
                }else
                {
                    MessageBox.Show("用户名已经存在");
                }
            }else
            {
                MessageBox.Show("输入不能为空!");
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindData(txtUserName.Text);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            userTemp = userinfoBll.GetModel(userID);
            userTemp.status = 1;
            bool result = userinfoBll.Update(userTemp);
            if(result==true)
            {
                MessageBox.Show("删除成功！");
                BindData();
                Clear();
            }else
            {
                MessageBox.Show("删除失败！");
            }

        }

        private void UserManager_Load(object sender, EventArgs e)
        {
            InitControl();
            BindData();
        }

        private void dgvUserInfo_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvUserInfo.CurrentRow.Selected = true;
            btnSave.Enabled = true;
            userID = (Int32)dgvUserInfo.CurrentRow.Cells[0].Value;
            txtUserName.Text = dgvUserInfo.CurrentRow.Cells[1].Value.ToString();
            txtPassword.Text = dgvUserInfo.CurrentRow.Cells[2].Value.ToString();
            string type= dgvUserInfo.CurrentRow.Cells[3].Value.ToString();
            int index=0;
            if(type.Equals("门诊用户"))
            {
                index = 0;
            }
            else if (type.Equals("仓库用户"))
            {
                index = 1;
            }
            cmbUserType.SelectedIndex = index;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            Clear();
        }

    }
}
